﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HUBS1.HCMS.Entity.OTA;
using HUBS1.HCMS.Entity;
using System.Data;
using HUBS1.HCMS.Bll.baseinfo;
using HCMS.Utility;
using System.Reflection;
namespace HUBS1.HCMS.Provider
{
    public class HotelAvail
    {
        public void QueryHotelAvail(HCRS_HotelAvailRQ Rqinfo, out HCRS_HotelAvailRS Rsinfo, out string errorcode)
        {
            Rsinfo = new HCRS_HotelAvailRS();
            errorcode = "";
            DataSetUtility dsu = new DataSetUtility();
            string[] Month_Field = new string[12] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

            try
            {
                #region 验证酒店、用户权限
                HCRS_Hotel hotel = new BllHCRS_HotelLocal().gethotel(int.Parse(Rqinfo.PropID));
                HCRS_TAUserInfo tauser = new BLLHCRS_TAUserinfo().getitem(int.Parse(Rqinfo.UserID.ToString()));
                if (hotel == null)
                    errorcode += "392,";//酒店不存在
                if (tauser == null)
                    errorcode += "243,";//用户不存在
                if (errorcode == "")
                {
                    string hotelgroupid = hotel.GroupID.ToString();
                    string hotelgroupids = "," + tauser.NoHotelGroup.Replace(" ", "") + ",";
                    if (hotelgroupids.IndexOf("," + hotelgroupid + ",") >= 0)
                        errorcode += "361,";//不在可预订集团酒店内
                }
                #endregion

                #region 查询价格计划
                //三级校验
                foreach (HCRS_HotelAvailRQStay hotelavailstay in Rqinfo.HotelAvailStay)
                {
                    DataTable P_dt = new DataTable();
                    P_dt.Columns.Add("AvailDate");
                    P_dt.Columns.Add("P_Av");
                    DateTime InDate = Convert.ToDateTime(hotelavailstay.Stay_Date);
                    DateTime OutDate = InDate.AddDays(int.Parse(hotelavailstay.Nights.ToString()));
                    int sYear = InDate.Year;
                    int eYear = OutDate.Year;
                    int sMonth = InDate.Month;
                    int eMonth = OutDate.Month;
                    for (int y = sYear; y <= eYear; y++)
                    {
                        List<HCRS_AIR> lair = new BllHCRS_AIR().getall("year=" + y + " and level='P' and property_id=" + Rqinfo.PropID);
                        if (lair.Count > 0)
                        {
                            for (int m = sMonth; m <= eMonth; m++)
                            {
                                string monthfield = Month_Field[m - 1];
                                HCRS_AIR air = lair[0];
                                DataSet paids = new DataSet();
                                MemberInfo[] mymember = air.GetType().GetMember(monthfield + "_AI");
                                if (mymember.Length > 0)
                                {
                                    if ((byte[])((System.Reflection.PropertyInfo)mymember[0]).GetValue(air, null) != null)
                                        paids = dsu.RetrieveDataSet((byte[])((System.Reflection.PropertyInfo)mymember[0]).GetValue(air, null));
                                }
                            }
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                errorcode += ex.Message + ",";
            }
        }
    }
}
